package com.example.network_android;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

import android.util.Log;



public class Server {
	
	private int port = 8888;
	private ServerSocket socket;

		
	public Server(){
		try {
				
			socket = new ServerSocket(port);
				
				
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
		
	public String listen(){
			
		try {
			Log.d("Network_android", "waiting for socket to accept...");
			Socket client = socket.accept();
			Log.d("Network_android", "... accepted socket.");
			ObjectInputStream in = new ObjectInputStream(client.getInputStream());
				
			Object o;
			try {
				o = in.readObject();

				
				
				ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
				
				
				if (o instanceof String){
					out.writeObject("Nachricht erhalten!");
					return (String)o;
				} 

				client.close();
				
				
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
		return null;
		}

	public ServerSocket getSocket() {
		return socket;
	}

	public void setSocket(ServerSocket socket) {
		this.socket = socket;
	}
	
		
}	
		